#!/bin/bash
die() {
	echo "Die on" $@
	exit 1
}

if [ -z $j_jenny_root_dir ]; then die "ASSERT: j_jenny_root_dir not defined."; fi

detect_os(){
	local os_name=`uname`

	case $os_name in
		Darwin)
			eval $1=mac
			;;
		Linux)
			eval $1=linux
			;;
		*)
			die "Unknow OS."
			;;
	esac
}

detect_os j_os_name

sync_cmake(){
	local cmake_dir=$j_jenny_root_dir/../tools/cmake/3.7.1
	[ -d $cmake_dir ] || mkdir -p $cmake_dir || die "Failed to create dir to download cmake"
	
	if [ -d $cmake_dir/.git ]; then
		(cd $cmake_dir; git pull)
	else
		(cd $cmake_dir; git clone git@gitee.com:wingfire/cmake-$j_os_name-3.7.1.git . )
	fi
}

detect_cmake() {
	detect_os os_name
	local cmake_path=$j_jenny_root_dir/../tools/cmake/3.7.1/bin/cmake
	if [ ! -x $cmake_path ]; then
		cmake_path=`type -p cmake` || die "Cmake not found"
	fi

	eval $1=$cmake_path
	unset os_name
}

extract_git_url(){
	local info=`(git remote -v|grep fetch) 2>/dev/null` || echo $2
	local s=${info% \(fetch\)};
	local url=${s#origin	}
	eval $1=$url
}

parse_args() {
	if [ $j_os_name = "mac" ]; then
		ARGS=`getopt c:t:d:p: "$@"`
		eval set -- "$ARGS"
	else
		ARGS=`getopt -o c:d: --long config:,target: -- "$@"`
		eval set -- "$ARGS"
	fi

	while [ -n $1 ]; do
		case "$1" in
			-c|--config)
				case "$2" in
					debug)
						j_config=Debug
						;;
					release) 
						j_config=Release
						;;
					*) die "unknown config"
						;;
				esac
				shift
				;;
			-t|--toolset) 
				case "$2" in 
					xcode)
						j_toolset=xcode
						;;
					make)
						j_toolset=make
						;;
					*)	die "Unknown toolset"
						;;
				esac
				shift
				;;
			-d|--target)
				j_target=$2
				shift
				;;
			-p|--platform) 
				case "$2" in 
					mac)
						j_platform=mac
						;;
					ios)
						j_platform=ios
						;;
					linux)
						j_platform=linux
						;;
					*)	die "Unknown platform"
						;;
				esac
				shift
				;;
			-a|--arch)
				j_architecture=$2	
				;;
			--) 
				shift
				j_config_path=$1
				break
				;;
			*) 
				die "Unknown flag $1"
				break
				;;
		esac
		shift
	done

	j_config=${j_config:-Debug}
	if [ $j_os_name = "mac" ]; then
		j_toolset=${j_toolset:-xcode}
		j_toolset_ver=${j_toolset_ver:-8}
		j_generator=${j_generator:-xcode}
		j_platform=${j_platform:-mac}
		j_architecture=${j_architecture:-x64}
	else
		j_toolset=${j_toolset:-make}
		j_toolset_ver=${j_toolset_ver:-4}
		j_generator=${j_generator:-make}
		j_platform=${j_platform:-linux}
		j_architecture=${j_architecture:-x64}
	fi

	if [ -n $j_target ]; then j_target="--target $j_target"; fi

	j_config_path=${j_config_path:-`pwd`}

	if [ -f $j_config_path ]; then 
		local dir=${j_config_path%/*}
		local filename=${j_config_path##*/}
		j_config_path=$(cd $dir; pwd)/$filename
		return; 
	elif [ -d $j_config_path ]; then
		j_config_path=$(cd $j_config_path; pwd)
	else
		die $j_config_path not found
	fi

	while [ -d "$j_config_path" ]; do
		if [ -f $j_config_path/jenny.cmake ]; then 
			j_config_path=$j_config_path/jenny.cmake;
			return
		else
			j_config_path=${j_config_path%/*}
		fi;
	done
	if [ ! "$j_no_jenny_cmake" = "1" ]; then
		die "jenny.cmake not found"
	fi
}

show_args(){
	echo j_config=$j_config
	echo j_toolset=$j_toolset
	echo j_platform=$j_platform
	echo j_config_path=$j_config_path

}

detect_jenny_cmake() {
	echo $0
}

get_cpu_count() {
	if [ $j_os_name = "mac" ]; then
		local cpu_count=`sysctl  machdep.cpu.thread_count | cut -d ' ' -f 2`
	else
		local cpu_count=`grep processor /proc/cpuinfo|wc -l`
	fi
	eval $1=$cpu_count
}

